package com.trtan.hr.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.trtan.hr.pojo.*;
import com.trtan.hr.service.HrService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: trtan
 * @create: 2021-05-02 09:14
 * @description:
 **/
@RestController
@RequestMapping("/system/hr")
public class HrController {
    Logger logger = LoggerFactory.getLogger(HrController.class);
    @Autowired
    private HrService hrService;
    @RequestMapping("/getAllHrs")
    public RespBean getAllRoles(@RequestBody JSONObject params) {
        logger.info(params.toString());
        Integer currentPage = params.getInteger("currentPage");
        Integer pageSize = params.getInteger("pageSize");
        String userName = params.getString("userName");
        RespBean respBean;
        try {
            RespPageEntity allHrs = hrService.queryAll(currentPage, pageSize, userName);
            respBean = RespBean.ok("", allHrs);
        } catch (Exception e) {
            e.printStackTrace();
            respBean = RespBean.error("服务器错误，请联系管理员！");
        }
        return respBean;
    }

    @RequestMapping("/addHr")
    public RespBean addHr(@RequestBody JSONObject params) {
        Hr hr = params.toJavaObject(Hr.class);
        RespBean respBean = null;
        try {
            hrService.addHr(hr);
            respBean = RespBean.ok("添加成功！", hr.getId());
        } catch (Exception e) {
            respBean = RespBean.error("服务器错误，请联系管理员！");
        }
        return respBean;
    }

    @RequestMapping("/deleteHr")
    public RespBean deleteHr(@RequestBody JSONObject params) {
        Integer hr_id = params.getInteger("hr_id");
        RespBean respBean;
        try {
            hrService.deleteHr(hr_id);
            respBean = RespBean.ok("删除成功！");
        } catch (Exception e) {
            respBean = RespBean.error("服务器错误，请联系管理员！");
        }
        return respBean;
    }

    @RequestMapping("/modifyHr")
    public RespBean modifyHr(@RequestBody JSONObject params) {
        Hr hr = params.toJavaObject(Hr.class);
        RespBean respBean = null;
        try {
            hrService.modifyHr(hr);
            respBean = RespBean.ok("修改成功！");
        } catch (Exception e) {
            respBean = RespBean.error("服务器错误，请联系管理员！");
        }
        return respBean;
    }
}
